import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/tmac/homePage.dart';
import 'package:flutter_demo/tmac/otherPage.dart';
import 'package:flutter_demo/tmac/personCenterPage.dart';
import 'package:flutter_demo/tmac/widgetPage.dart';



class MainMenuPageStatefulWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new MainMenuPageState();
  }
}

class MainMenuPageState extends State<MainMenuPageStatefulWidget> {
  int _currentIndex = 0;
  PageController _pageController;

  List<Widget> lists;
  DateTime _lastPressedAt; //上次点击时间

  void onTap(int index) {
    _pageController.jumpToPage(index);
  }

  void _pageChange(int index) {
    if (index != _currentIndex) {
      setState(() {
        _currentIndex = index;
      });
    }
  }
  @override
  void initState() {
    super.initState();
    lists = List()
      ..add(HomePageStatefulWidget())
      ..add(WidgetStatefulPage())
      ..add(OtherPage())
      ..add(PersonCenterStatefulPage());
    _pageController = new PageController(initialPage: 0);
  }

  @override
  void dispose() {
    super.dispose();
    _pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new WillPopScope(child: new Scaffold(
      body: PageView.builder(
          controller: _pageController,
          physics: NeverScrollableScrollPhysics(),
          onPageChanged: _pageChange,
          itemCount: lists.length,
          itemBuilder: (context, index) => lists[index]),
      bottomNavigationBar: new BottomNavigationBar(
        currentIndex: this._currentIndex,
        fixedColor: Colors.blue,
        selectedFontSize: 15,
        selectedLabelStyle: TextStyle(fontWeight: FontWeight.bold),
        unselectedFontSize: 12,
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(
              icon: Icon(
                Icons.account_balance,
                size: _currentIndex == 0 ? 30 : 20,
              ),
              title: Text("首页")),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.widgets,
                size: _currentIndex == 1 ? 30 : 20,
              ),
              title: Text("组件")),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.all_inclusive,
                size: _currentIndex == 2 ? 30 : 20,
              ),
              title: Text("其他")),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.person_pin,
                size: _currentIndex == 3 ? 30 : 21,
              ),
              title: Text("我的"))
        ],
        onTap:onTap,
      ),
    ), onWillPop:  () async {
      if (_lastPressedAt == null ||
          DateTime.now().difference(_lastPressedAt) > Duration(seconds: 1)) {
        //两次点击间隔超过1秒则重新计时
        _lastPressedAt = DateTime.now();
        return false;
      }
      return true;
    });

  }

}
